//
//  DiffusionAnimationView.m
//  BalanceCar
//
//  Created by 郑信鸿 on 17/7/1.
//  Copyright © 2017年 郑信鸿. All rights reserved.
//

#import "DiffusionAnimationView.h"

 static NSString *diffusionAnimationKey = @"diffusionAnimation";

@interface DiffusionAnimationView ()

@property(nonatomic, assign)BOOL isAnimantioning;

@end

@implementation DiffusionAnimationView

- (instancetype)init{
    
    if (self = [super init]) {
        self.isAnimantioning = NO;
        UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 180, 180) cornerRadius:90];
        CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
        
        shapeLayer.frame = CGRectMake(0, 0, 180, 180);
        shapeLayer.anchorPoint = CGPointMake(0, 0);
        shapeLayer.position = CGPointMake(0, 0);
        shapeLayer.path = bezierPath.CGPath;
        //.mask = shapeLayer;
    }
    return self;
}


- (void)waveAnimationLayerWithRadius:(CGFloat)radius color:(UIColor *)color duration:(CGFloat)duration lineWidth:(CGFloat)lineWith beginTime:(CGFloat)beginTime{
    CAShapeLayer *annulusLayer = [CAShapeLayer layer];
    CGFloat x = (kUIScreenWidth - radius * 2)/2.0;
    CGFloat y = (320 - radius * 2)/2.0;
    annulusLayer.frame = CGRectMake(x, y, radius * 2, radius * 2);
    annulusLayer.fillColor = [[UIColor clearColor] CGColor];
    annulusLayer.strokeColor = color.CGColor;
    annulusLayer.lineWidth = lineWith;
    annulusLayer.opacity = 0;
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(radius, radius) radius:radius startAngle:0 endAngle:M_PI * 2 clockwise:YES];
    annulusLayer.path = [path CGPath];
    [self.superview.layer insertSublayer:annulusLayer below:self.layer];
    CAAnimationGroup * animationGroup = [CAAnimationGroup animation];
    animationGroup.duration = duration;
    animationGroup.repeatCount = INFINITY; //重复无限次
    animationGroup.removedOnCompletion = NO;
    
    CAMediaTimingFunction *defaultCurve = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
    animationGroup.timingFunction = defaultCurve;
    
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale.xy"];
    scaleAnimation.fromValue = @1.0; //开始的大小
    scaleAnimation.toValue = @2.0; //最后的大小
    scaleAnimation.duration = duration;
    scaleAnimation.removedOnCompletion = NO;
    CABasicAnimation *lineWidthAnimation = [CABasicAnimation animationWithKeyPath:@"lineWidth"];
    lineWidthAnimation.fromValue = @10; //开始的大小
    lineWidthAnimation.toValue = @0.0; //最后的大小
    lineWidthAnimation.duration = duration;
    lineWidthAnimation.removedOnCompletion = NO;
    
    CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnimation.fromValue = @0.4; //开始的大小
    opacityAnimation.toValue = @0.0; //最后的大小
    opacityAnimation.duration = duration;
    opacityAnimation.removedOnCompletion = NO;
  
    
    animationGroup.animations = @[scaleAnimation, opacityAnimation,lineWidthAnimation];
    CGFloat addTime = [annulusLayer convertTime:CACurrentMediaTime() fromLayer:nil];
    animationGroup.beginTime = beginTime + addTime;
    //animationGroup.repeatDuration = 0.8;
    [annulusLayer addAnimation:animationGroup forKey:diffusionAnimationKey];
 
}


- (void)stopAnimation{
    
    NSArray *layerArr = [NSArray arrayWithArray:self.superview.layer.sublayers];
    for (CALayer *layer in layerArr) {
        if ([layer.animationKeys containsObject:diffusionAnimationKey]) {
            [layer removeAllAnimations];
            [layer removeFromSuperlayer];
        }
    }
    self.isAnimantioning = NO;
}


- (void)startAnimation{
    
    if (self.isAnimantioning) {
        [self stopAnimation];
    }
    [self waveAnimationLayerWithRadius:100 color:RGBColor(213, 213, 213) duration:2.4 lineWidth:15 beginTime:0];
     [self waveAnimationLayerWithRadius:100 color:RGBColor(213, 213, 213) duration:2.4 lineWidth:15 beginTime:0.6];
    [self waveAnimationLayerWithRadius:100 color:RGBColor(213, 213, 213) duration:2.4 lineWidth:15 beginTime:1.2];
     [self waveAnimationLayerWithRadius:100 color:RGBColor(213, 213, 213) duration:2.4 lineWidth:15 beginTime:1.8];
    
    self.isAnimantioning = YES;

}

@end
